﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace PartCompile
{
    class Program
    {
        static void Main(string[] args)
        {
            ///要分部编译的文件夹路径
            string path = @"E:\zhangweixing\Work\SMR\SRM\Solution\FounderPKURG\Founder.PKURG.CustomPage\EvaluateResult";
            if (args.Length>0)
            {
                Compile(args[0]);
            }
            //Compile(path);
        }

        private static void Compile(string path)
        {
            string publishDir = Path.Combine(path, "publish");
            if (Directory.Exists(publishDir))
            {
                new FileInfo(publishDir).IsReadOnly = false;
                new FileInfo(publishDir).Attributes = FileAttributes.Normal;
                Directory.Delete(publishDir, true);
                System.Threading.Thread.Sleep(1000);
            }

            Directory.CreateDirectory(publishDir);


            string outDllPath = Path.Combine(publishDir, Path.GetFileName(path) + ".dll");

            string[] csFiles = Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories);

            string dllPath = Path.Combine(Path.GetDirectoryName(path), "bin");
            if (!Directory.Exists(dllPath))
            {
                dllPath = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(path)), "bin");
            }

            string[] dllFiles = Directory.GetFiles(dllPath, "*.dll", SearchOption.TopDirectoryOnly);

            StringBuilder content = new StringBuilder();
           // content.Append(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe  ");
            content.AppendFormat("{0} /out:{1} ", @"/target:library", outDllPath);
            content.Append("/reference:");
            foreach (string item in dllFiles)
            {
                content.AppendFormat("{0},", item);
            }
            content.Remove(content.Length - 1, 1);

            foreach (string item in csFiles)
            {
                content.AppendFormat(" {0} ", item);
            }

            //Console.WriteLine(content.ToString());

            string[] aspxFiles = Directory.GetFiles(path, "*.aspx", SearchOption.AllDirectories);
            foreach (string item in aspxFiles)
            {
                string newPath = Path.Combine(publishDir, item.Replace(path + "\\", ""));
                File.Copy(item, newPath, true);
                new FileInfo(newPath).Attributes = FileAttributes.Normal;
            }
            //using (StreamWriter sw = new StreamWriter(Console.OpenStandardInput()))
            //{
            //    sw.WriteLine(content.ToString());
            //}
            //content.Append("\n pause");
            Process proc = new Process();
            //proc.StartInfo = new ProcessStartInfo(@"C:\Windows\System32\cmd.exe", content.ToString());
            proc.StartInfo = new ProcessStartInfo(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe", content.ToString());


            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
           // proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
            //proc.StartInfo.CreateNoWindow = false;
            proc.Start();

            Console.WriteLine(proc.StandardOutput.ReadToEnd());
            Console.WriteLine(proc.StandardError.ReadToEnd());
            proc.WaitForExit();
            File.WriteAllText("E:\\123.txt", content.ToString());
        }

        static void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine(e.Data);
        }
    }
}
